使用OpenSSL TLS1.3 Demo

您所在的位置:网站首页 cc g3皮头 使用OpenSSL TLS1.3 Demo

使用OpenSSL TLS1.3 Demo

2023-06-18 13:41| 来源: 网络整理| 查看: 265

更新OpenSSL库 目前我Ubuntu上的OpenSSL库版本比较低,不支持TLS1.3.

root@ubuntu:~/openssl/openssl-1.1.1c# openssl version OpenSSL 1.0.2g 1 Mar 2016 root@ubuntu:~/openssl/openssl-1.1.1c# openssl ciphers -v 'ALL:COMPLEMENTOFALL' | grep TLSv1.3 root@ubuntu:~/openssl/openssl-1.1.1c#

下载最新OpenSSL版本,从官网上的值目前最新的稳定版本是1.1.1系列。我下载了openssl-1.1.1c.tar.gz,链接地址:https://www.openssl.org/source/openssl-1.1.1c.tar.gz 放到Ubuntu上解压。 根据Cookbook介绍按部就班,为了不影响现有OpenSSL库,配置好前缀。 ./config --prefix=/usr/local/openssl-1.1.1c --openssldir=/usr/local/openssl-1.1.1c enable-ec_nistp_64_gcc_128 make depend make sudo make install 先看一下效果:

root@ubuntu:~/openssl/openssl-1.1.1c# cd /usr/local/openssl-1.1.1c/ root@ubuntu:/usr/local/openssl-1.1.1c# ./bin/openssl version root@ubuntu:/usr/local/openssl-1.1.1c/bin# ./openssl version ./openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

报错找不到libssl.so.1.1,先临时配置一下环境变量:

root@ubuntu:/usr/local/openssl-1.1.1c# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib root@ubuntu:/usr/local/openssl-1.1.1c# ./bin/openssl version OpenSSL 1.1.1c 28 May 2019

版本是正确的,再看一下加密套件支持情况:

root@ubuntu:/usr/local/openssl-1.1.1c# ./bin/openssl ciphers -v 'ALL:COMPLEMENTOFALL' | grep TLSv1.3 TLS_AES_256_GCM_SHA384 TLSv1.3 Kx=any Au=any Enc=AESGCM(256) Mac=AEAD TLS_CHACHA20_POLY1305_SHA256 TLSv1.3 Kx=any Au=any Enc=CHACHA20/POLY1305(256) Mac=AEAD TLS_AES_128_GCM_SHA256 TLSv1.3 Kx=any Au=any Enc=AESGCM(128) Mac=AEAD

将新版本的openssl配置到系统。 移除原有OpenSSL版本(先搜索查看原有的ssl库在哪些地方,然后对应配置一下):

mv /usr/bin/openssl /usr/bin/openssl.1.0.2g ln -s /usr/local/openssl-1.1.1c/bin/openssl /usr/bin/openssl mv /usr/include/openssl/ /usr/include/openssl.1.0.2g ln -s /usr/local/openssl-1.1.1c/include/openssl /usr/include/openssl rm -rf /usr/lib/x86_64-linux-gnu/libssl.so ln -s /usr/local/openssl-1.1.1c/lib/libssl.so.1.1 /usr/lib/x86_64-linux-gnu/libssl.so rm /usr/lib/x86_64-linux-gnu/libcrypt.so ln -s /usr/local/openssl-1.1.1c/lib/libcrypto.so.1.1 /usr/lib/x86_64-linux-gnu/libcrypt.so cp /usr/local/openssl-1.1.1c/lib/libssl.so.1.1 /lib/x86_64-linux-gnu/ cp /usr/local/openssl-1.1.1c/lib/libcrypto.so.1.1 /lib/x86_64-linux-gnu/ mv /usr/lib/x86_64-linux-gnu/libssl.a /usr/lib/x86_64-linux-gnu/libssl.a.1.0.2g cp /usr/local/openssl-1.1.1c/lib/libssl.a /usr/lib/x86_64-linux-gnu/ mv /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libcrypto.a.1.0.2g cp /usr/local/openssl-1.1.1c/lib/libcrypto.a /usr/lib/x86_64-linux-gnu/ cp /usr/local/openssl-1.1.1c/lib/libssl.so.1.1 /usr/lib/i386-linux-gnu/ cp /usr/local/openssl-1.1.1c/lib/libssl.so.1.1 /lib/i386-linux-gnu/ cp /usr/local/openssl-1.1.1c/lib/libcrypto.so.1.1 /lib/i386-linux-gnu/ cp /usr/local/openssl-1.1.1c/lib/libcrypto.so.1.1 /usr/lib/i386-linux-gnu/ rm -rf /usr/lib/i386-linux-gnu/libcrypto.so ln -s /lib/i386-linux-gnu/libcrypto.so.1.1 /usr/lib/i386-linux-gnu/libcrypto.so rm -rf /usr/lib/x86_64-linux-gnu/libcrypto.so ln -s /lib/x86_64-linux-gnu/libcrypto.so.1.1 /usr/lib/x86_64-linux-gnu/libcrypto.so rm -rf /usr/lib/i386-linux-gnu/libssl.so ln -s /lib/i386-linux-gnu/libssl.so.1.1 /usr/lib/i386-linux-gnu/libssl.so

在一个新的Shell中运行,openssl版本已经更新

root@ubuntu:~/chromium/src# openssl version OpenSSL 1.1.1c 28 May 2019 root@ubuntu:~/chromium/src# openssl ciphers -v 'ALL:COMPLEMENTOFALL' | grep TLSv1.3 TLS_AES_256_GCM_SHA384 TLSv1.3 Kx=any Au=any Enc=AESGCM(256) Mac=AEAD TLS_CHACHA20_POLY1305_SHA256 TLSv1.3 Kx=any Au=any Enc=CHACHA20/POLY1305(256) Mac=AEAD TLS_AES_128_GCM_SHA256 TLSv1.3 Kx=any Au=any Enc=AESGCM(128) Mac=AEAD

重新编译Server和Client,出现大量错误:DEPRECATEDIN_1_1_0等符号找不到定义。 这个符号是在opensslconf.in.h中定义的,在Make的时候会自动生成为opensslconf.h(竟然这样的骚操作,本地代码上根本看不到这个opensslconf.h)。但是即使这样,系统的openssconf.h也已经正常被生成,应该不会有问题。搜索一下系统,发现还有其他的目录下存在这个文件,需要替换掉。 存在opensslconf.h的目录有:/usr/include/x86_64-linux-gnu/openssl/,/usr/include/i386-linux-gnu/openssl/ 替换为最新的文件解决问题。

重新编译server和client,发现有一些函数已经处于未定义状态:

root@ubuntu:/home/zengyi/data/openssl# gcc Simple_TLS_Server.c -lssl -lcrypto -o tls_server /tmp/ccssBesj.o: In function `init_openssl': Simple_TLS_Server.c:(.text+0xbf): undefined reference to `SSL_load_error_strings' Simple_TLS_Server.c:(.text+0xc4): undefined reference to `SSL_library_init' /tmp/ccssBesj.o: In function `cleanup_openssl': Simple_TLS_Server.c:(.text+0xd0): undefined reference to `EVP_cleanup' /tmp/ccssBesj.o: In function `create_context': Simple_TLS_Server.c:(.text+0xe0): undefined reference to `SSLv23_server_method' collect2: error: ld returned 1 exit status

查看源码,发现这些函数确实已经废弃,版本号小于1.0.1才有用:

# if OPENSSL_API_COMPAT < 0x10100000L # define SSL_load_error_strings() \ OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS \ | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL) # endif

从SSL_library_init()手册看,SSL_load_error_strings()与SSL_library_init(),EVP_Cleanup()接口已经被OPENSSL_init_ssl()替代。SSLv23_server_method()接口已经被TLS_server_method()接口替代,TLS_server_method()接口已经摒弃了具体的协议约束。

The SSL_library_init() and OpenSSL_add_ssl_algorithms() functions were deprecated in OpenSSL 1.1.0 by OPENSSL_init_ssl(). Calling this function will explicitly initialise BOTH libcrypto and libssl. The OpenSSL_add_all_algorithms(), OpenSSL_add_all_ciphers(), OpenSSL_add_all_digests(), and EVP_cleanup(), functions were deprecated in OpenSSL 1.1.0 by OPENSSL_init_crypto(). SSLv23_method(), SSLv23_server_method() and SSLv23_client_method() were deprecated and the preferred TLS_method(), TLS_server_method() and TLS_client_method() functions were introduced in OpenSSL 1.1.0. All version-specific methods were deprecated in OpenSSL 1.1.0.

int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings);

将SSL_load_error_strings();OpenSSL_add_ssl_algorithms();函数替换为OPENSSL_init_ssl,将 EVP_cleanup()注释掉,将SSLv23_server_method();修改为TLS_server_method()。编译成功! 运行tls_server 运行 openssl s_client 可以看到使用了TLSv1.3握手! 抓包如下:

image.png


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3